package com.woniuxy.store.app.apiImpl;

import cn.hutool.core.bean.BeanUtil;
import com.woniuxy.cloud.mall95.log.Log;
import com.woniuxy.store.app.util.GetTypeByHead;
import com.woniuxy.store.app.util.StoreIdUtil;
import com.woniuxy.store.client.StoreInfoClient;
import com.woniuxy.store.client.param.ModStoreInfoParam;
import com.woniuxy.store.client.result.StoreInfoDto;
import com.woniuxy.store.infra.model.StoreInfo;
import com.woniuxy.store.service.StoreInfoService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import result.Result;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.io.IOException;

@RestController
@RequestMapping("/store/manage")
public class StoreInfoClientImpl implements StoreInfoClient {

    @Resource
    private StoreInfoService storeInfoService;

    @Override
    @Log("获取当前登录商铺信息")
    public Result<StoreInfoDto> getStoreInfo() {
        StoreInfo storeInfo = storeInfoService.getById(StoreIdUtil.getFromToken());
        StoreInfoDto dto = BeanUtil.toBean(storeInfo, StoreInfoDto.class);
        return Result.success(dto);
    }

    @Override
    @Log("根据商家ID获取商家信息")
    public Result<StoreInfoDto> getInfoById(Integer storeId) {
        StoreInfo info = storeInfoService.getById(storeId);
        StoreInfoDto dto = BeanUtil.toBean(info, StoreInfoDto.class);
        return Result.success(dto);
    }

    @Override
    @Log("修改商家信息")
    public Result modifyStoreInfo(@Valid ModStoreInfoParam param) {
        StoreInfo storeInfo = BeanUtil.toBean(param, StoreInfo.class);
        storeInfo.setStoreId(StoreIdUtil.getFromToken());
        storeInfoService.modifyStoreInfo(storeInfo);
        return Result.success();
    }

    @Override
    @Log("上传营业执照")
    public Result uploadLicense(MultipartFile file) throws Exception {
        String fileType = GetTypeByHead.getFileHeader(file);
        System.out.println(fileType);
        if (!(fileType.equals("jpg")||fileType.equals("png")||fileType.equals("tif"))){
            throw new Exception("请上传图片类型的文件");
        }
        storeInfoService.uplicense(file,StoreIdUtil.getFromToken());
        return Result.success();
    }

    @Override
    @Log("上传餐饮许可证")
    public Result uploadPermit(MultipartFile file) throws Exception {
        String fileType = GetTypeByHead.getFileHeader(file);
        if (!(fileType.equals("jpg")||fileType.equals("png")||fileType.equals("tif"))){
            throw new Exception("请上传图片类型的文件");
        }
        storeInfoService.uploadPermit(file,StoreIdUtil.getFromToken());
        return Result.success();
    }
}
